package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.UpdateSqIdcardInfoReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.UpdateSqIdcardInfoCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.model.sqidcard.IdcardInfo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.sqidcard.IdcardInfoWrapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_SqUpdateIdcardInfo {

	private static UpdateSqIdcardInfoReq updateSqIdcardInfoReq;
	private static UpdateSqIdcardInfoCall updateSqIdcardInfoCall;

	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改身份证信息接口");
	}

	@Before
	public void caseUp() {
		updateSqIdcardInfoReq = new UpdateSqIdcardInfoReq();
		updateSqIdcardInfoCall = new UpdateSqIdcardInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateSqIdcardInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public static void verify(long orderId) {

	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_updateSqIdcardInfoReq_001() {
		Logger.start(true, "订单不存在，修改身份证信息失败");
		try {

			updateSqIdcardInfoReq.setOrderId(1L);
			updateSqIdcardInfoReq.setUserId(20238699L);
			updateSqIdcardInfoReq.setIdcardName(TestDataManager
					.getChineseName());
			updateSqIdcardInfoReq.setIdcardNo(TestDataManager.getCardId(18, 1));

			updateSqIdcardInfoCall.setData(updateSqIdcardInfoReq);
			updateSqIdcardInfoCall.callService();

			Logger.verifyEquals(false, updateSqIdcardInfoCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(101, updateSqIdcardInfoCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals("订单不存在", updateSqIdcardInfoCall.getMsg(),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	// sit无法推单，修改身份证失败
	public void Tc_updateSqIdcardInfoReq_003() {
		Logger.start(true, "神器订单支付成功后，修改身份证信息成功");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithPurchaserIdcardId(productBean, 3L);

			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString(),
					productBean.getCurrency());

			Thread.sleep(5000);

			updateSqIdcardInfoReq.setOrderId(orderId);
			updateSqIdcardInfoReq.setUserId(20238699L);
			updateSqIdcardInfoReq.setIdcardName(TestDataManager
					.getChineseName());
			updateSqIdcardInfoReq.setIdcardNo(TestDataManager.getCardId(18, 1));

			updateSqIdcardInfoCall.setData(updateSqIdcardInfoReq);
			updateSqIdcardInfoCall.callService();

			Logger.verifyEquals(true, updateSqIdcardInfoCall.getSuccess(),
					"验证success");

			String newKey = "123456789";

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
			IdcardInfo idcardInfo = idcardInfoWrapper
					.selectByNameAndNoAndUserId(
							updateSqIdcardInfoReq.getIdcardName(),
							CipherUtil.encryptAES(newKey,
									updateSqIdcardInfoReq.getIdcardNo()),
							userId);

			Logger.verifyEquals(orderPo.getPurchaserIdcardId(),
					idcardInfo.getIdcard_id(), "验证idcardId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_updateSqIdcardInfoReq_004() {
		Logger.start(true, "神器订单未支付，修改身份证信息失败");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithPurchaserIdcardId(productBean, 3L);

			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();

			Thread.sleep(5000);

			updateSqIdcardInfoReq.setOrderId(orderId);
			updateSqIdcardInfoReq.setUserId(userId);
			updateSqIdcardInfoReq.setIdcardName(TestDataManager
					.getChineseName());
			updateSqIdcardInfoReq.setIdcardNo(TestDataManager.getCardId(18, 1));

			updateSqIdcardInfoCall.setData(updateSqIdcardInfoReq);
			updateSqIdcardInfoCall.callService();

			Logger.verifyEquals(false, updateSqIdcardInfoCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(102, updateSqIdcardInfoCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals("订单未支付", updateSqIdcardInfoCall.getMsg(),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static void main(String[] args) throws Exception {

		Logger.comment(CipherUtil
				.decryptAES("123456789",
						"2683110E1208FDBFB14468AA45F84CFC446F4F6E4BBB7AF571FA0E98ACFCFDBC"));
	}
}
